package com.nvn.searchapp;

import java.io.File;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void onContacts(View v) {
		Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);
		Toast.makeText(getBaseContext(), cursor.getCount()+" contacts!", Toast.LENGTH_LONG).show();
	}

	public void onMusics(View v) {
		Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,null, null);
		Toast.makeText(getBaseContext(), cursor.getCount()+" songs!", Toast.LENGTH_LONG).show();
	}

	public void onMessages(View v) {
		Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox/"), null, null,null, null);
		Toast.makeText(getBaseContext(), cursor.getCount()+" smses!", Toast.LENGTH_LONG).show();
	}

	public void onFiles(View v) {
		Collection<File> files = FileUtils.listFiles(Environment.getExternalStorageDirectory(),
				new RegexFileFilter(".*IMG_.*"), TrueFileFilter.TRUE);

		Toast.makeText(getBaseContext(), files.size()+" files!", Toast.LENGTH_LONG).show();
		
		for (File f : files) {
			Log.i("---FILE---", f.getName());
		}
	}

	public void onSearch(View v) {
		Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
		intent.putExtra(SearchManager.QUERY, "my love will go on"); // query
																	// contains
																	// search
																	// string
		startActivity(intent);
	}

	public void onPhotos(View v) {
		Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null,null, "date_added DESC");
		Toast.makeText(getBaseContext(), cursor.getCount()+" photos!", Toast.LENGTH_LONG).show();
	}

	
	
}
